class Bus {
  queue: { [key: string]: Function[] }
  constructor() {
    // 收集订阅信息,调度中心
    this.queue = {}
  }

  // 订阅
  $on(name: string, fn: Function) {
    this.queue[name] = this.queue[name] || []
    this.queue[name].push(fn)
  }

  // 发布
  $emit(name: string, data?: any) {
    if (this.queue[name]) {
      this.queue[name].forEach((fn: Function) => {
        fn(data)
      })
    }
  }

  // 取消订阅
  $off(name: string) {
    if (this.queue[name]) {
      delete this.queue[name]
    }
  }
}
export default Bus
